﻿@namespace ThingsGateway.Demo
@inherits LayoutComponentBase
@implements IDisposable
<CascadingValue Value="IsMobile" Name="IsMobile">
    <CascadingValue Value="BaseChanged" Name="BaseChanged">
        <MApp>
            <MErrorHandler>
                @if (AppService.InitSuccess)
                {
                    @Body
                }
            </MErrorHandler>
        </MApp>
    </CascadingValue>
</CascadingValue>

@code {
    public bool IsMobile { get; set; }
    public bool BaseChanged { get; set; }
    [Inject]
    public BlazorAppService AppService { get; set; }
    [Inject]
    public MasaBlazor MasaBlazor { get; set; }
    protected override void OnInitialized()
    {
        base.OnInitialized();
        MasaBlazor.BreakpointChanged += BreakpointOnOnUpdate;
    }
    protected async override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var data = await AppService.InitAll();
            if (data)
            {
                BaseChanged = !BaseChanged;
                await InvokeAsync(StateHasChanged);
            }
        }
        await base.OnAfterRenderAsync(firstRender);
    }
    private void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e)
    {
        IsMobile = MasaBlazor.Breakpoint.Mobile;
        if (e.MobileChanged)
        {
            StateHasChanged();
        }
    }
    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    public void Dispose()
    {
        MasaBlazor.BreakpointChanged -= BreakpointOnOnUpdate;
    }
}